#include "TestCommon.h"
#include "FastDB/CellDB.h"

BOOST_AUTO_TEST_SUITE(TestCellDB)

BOOST_AUTO_TEST_CASE(TestCellDBMain)
{
    char szBuffer[256] = { 0 };
    FileSystem_Init();

    unlink("./test-cell.db");
    CellDB myTest1("./test-cell.db");

    SizeType iFile1 = myTest1.CreateFile();
    BOOST_REQUIRE(0 == iFile1);
    SizeType iFile2 = myTest1.CreateFile();
    BOOST_REQUIRE(1 == iFile2);

    BOOST_REQUIRE(6 == myTest1.WriteFile(iFile1, "preved", 6, 0)); 
    BOOST_REQUIRE(6 == myTest1.WriteFile(iFile1, "medved", 6, 6)); 

    BOOST_REQUIRE(6 == myTest1.WriteFile(iFile2, "hello ", 6, 0)); 
    BOOST_REQUIRE(6 == myTest1.WriteFile(iFile2, "world!", 6, 6)); 

    BOOST_REQUIRE(12 == myTest1.ReadFile(iFile1, szBuffer, 12, 0)); 
    BOOST_REQUIRE(0 == memcmp(szBuffer, "prevedmedved", 12));
    BOOST_REQUIRE(12 == myTest1.ReadFile(iFile2, szBuffer, 12, 0)); 
    BOOST_REQUIRE(0 == memcmp(szBuffer, "hello world!", 12));

    CellDB myTest2("./test-cell.db"); 
    BOOST_REQUIRE(12 == myTest2.ReadFile(iFile1, szBuffer, 12, 0)); 
    BOOST_REQUIRE(0 == memcmp(szBuffer, "prevedmedved", 12));
    BOOST_REQUIRE(12 == myTest2.ReadFile(iFile2, szBuffer, 12, 0)); 
    BOOST_REQUIRE(0 == memcmp(szBuffer, "hello world!", 12));

    BOOST_REQUIRE(6 == myTest2.WriteFile(iFile1, "world!", 6, 6)); 
    BOOST_REQUIRE(6 == myTest2.WriteFile(iFile2, "medved", 6, 6)); 

    BOOST_REQUIRE(12 == myTest1.ReadFile(iFile1, szBuffer, 12, 0)); 
    BOOST_REQUIRE(0 == memcmp(szBuffer, "prevedworld!", 12));
    BOOST_REQUIRE(12 == myTest1.ReadFile(iFile2, szBuffer, 12, 0)); 
    BOOST_REQUIRE(0 == memcmp(szBuffer, "hello medved", 12));


    for(SizeType i = iFile2 + 1; i < 1024; ++i)
    {
            SizeType tmp = myTest1.CreateFile();
            BOOST_REQUIRE(i == tmp); 
    }

    FileSystem_Free();
};

BOOST_AUTO_TEST_SUITE_END()
